/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bsc.war.form;

import bsc.ejb.entity.PersonScoreDomain;
import bsc.ejb.entity.ScoreDomain;
import bsc.ejb.entity.ScoreView;
import bsc.ejb.entity.TopicScoreView;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author luisZavaleta
 */
public class DeleteScoreDomain extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");
            ScoreDomain scoreDomain = delegate.findScoreDomain(Long.valueOf(request.getParameter("id")));
            ScoreView scoreView = scoreDomain.getScoreView();

            List<PersonScoreDomain> personScoreDomains = delegate.findPersonScoreDomain(scoreDomain);
            for (PersonScoreDomain personScoreDomain : personScoreDomains) {
                delegate.deletePersonScoreDomain(personScoreDomain);
            }

            List<TopicScoreView> topicScoreViews = delegate.findTopicScoreView(scoreView);
            for (TopicScoreView topicScoreView : topicScoreViews) {
                delegate.deleteTopicScoreView(topicScoreView);
            }

            scoreDomain.setScoreView(null);
            delegate.updateScoreDomain(scoreDomain);

            scoreView.setScoreDomain(null);
            delegate.updateScoreView(scoreView);
            
            delegate.deleteScoreDomain(scoreDomain);
            delegate.deleteScoreView(scoreView);

            boolean deleted = true;

            request.setAttribute("deleted", deleted);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return  new ModelAndView("deleteScoreDomain");

    }

}
